<?php
// +----------------------------------------------------------------------
// | 有小象汇 自研正版源码 赋能开发者
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 http://www.uxphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 有小象汇并不是自由软件，未经许可不能去掉有小象汇相关版权
// +----------------------------------------------------------------------
// | Author:  有小象汇
// +----------------------------------------------------------------------

namespace app\services\car;


use app\dao\car\BaseCityFeeDao;
use app\model\car\BaseCity;
use app\services\BaseServices;
use crmeb\exceptions\AdminException;
use crmeb\services\FormBuilder as Form;
use think\facade\Route as Url;

class BaseCityFeeServices extends BaseServices
{
    public function __construct(BaseCityFeeDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * 列表
     * @param array $where
     * @param bool $is_page
     * @return array
     */
    public function getList(array $where = [],bool $is_page = false){
        $page = $limit = 0;
        if($is_page) [$page, $limit] = $this->getPageValue();
        $list = $this->dao->getList($where,$page,$limit,$is_page);
        return $list;
    }

    /**
     * 添加|修改
     * @param int $id
     * @return array
     * @throws \FormBuilder\Exception\FormBuilderException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add(int $id){
        $cityFee = $this->dao->get($id);
        $city = BaseCity::field('id as value,city_name as label')->where(['is_del'=>0])->select()->toArray();
        $field = array();
        $title = $id ? '修改' : '添加';
        $field[] = Form::hidden('id', $id);
        $field[] = Form::select('start_city_id', '取车城市', $cityFee['start_city_id']??'')->setOptions($city);
        $field[] = Form::select('end_city_id', '还车城市', $cityFee['end_city_id']??'')->setOptions($city);
        $field[] = Form::input('city_fee', '还车费用', $cityFee['city_fee']??'');
        $field[] = Form::input('city_hour', '占用库存小时', $cityFee['city_hour']??'');
        return create_form($title, $field, Url::buildUrl('base/city_fee/save'), 'POST');
    }

    /**
     * 保存
     * @param int $id
     * @param array $data
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function save(int $id, array $data){
        $cityFee = $this->dao->getOne(['start_city_id' => $data['start_city_id'],'end_city_id' => $data['end_city_id'],'is_del'=>0]);
        if ($id) {
            if (!$this->dao->get($id)) throw new AdminException('数据不存在');
            if ($cityFee && $id != $cityFee['id']) throw new AdminException('该线路已设置');
            if (!$this->dao->update($id, $data)) throw new AdminException('修改失败或者您没有修改什么');
        } else {
            unset($data['id']);
            if ($cityFee) throw new AdminException('该线路已设置');
            $data['add_time'] = time();
            if (!$this->dao->save($data)) throw new AdminException('添加失败');
        }
        return true;
    }

    /**
     * 删除
     * @param int $id
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delCityFee(int $id){
        if (!$this->dao->get($id)) throw new AdminException('未查询到数据');
        if (!$this->dao->update($id,['is_del'=>1,'delete_time'=>time()])) throw new AdminException('删除失败,请稍候再试!');
        return '删除成功';
    }
}
